<!-- 未采用封装 -->
<template>
  <div class="info-box">
    <el-button type="primary" class="add-stu" @click="addStudent"
      >新增</el-button
    >
    <!-- 表格区域 -->
    <el-table :data="comData" border v-loading="loading" style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="sex_text" label="性别" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" align="center">
      </el-table-column>
      <el-table-column prop="father" label="父亲" align="center">
      </el-table-column>
      <el-table-column prop="mother" label="母亲" align="center">
      </el-table-column>
      <el-table-column prop="time" label="入学时间" align="center">
      </el-table-column>
      <el-table-column prop="address" label="地址" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="联系方式" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <div class="operate">
            <el-button type="primary" size="mini" @click="edit(scope.row)"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="del(scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <PageWeb
      @getTableData="getTableData"
      :total="total"
      :tableData="tableData"
    ></PageWeb>
    <!-- 弹出对话框 -->
    <el-dialog
      :title="state ? '添加学生信息' : '修改学生信息'"
      v-model="dialogFormVisible"
      width="500px"
    >
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
          <el-input v-model="form.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="父亲姓名"
          :label-width="formLabelWidth"
          prop="father"
        >
          <el-input v-model="form.father" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="母亲姓名"
          :label-width="formLabelWidth"
          prop="mother"
        >
          <el-input v-model="form.mother" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="入学时间"
          :label-width="formLabelWidth"
          prop="time"
        >
          <el-date-picker
            v-model="form.time"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="家庭地址"
          :label-width="formLabelWidth"
          prop="address"
        >
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="联系方式"
          :label-width="formLabelWidth"
          prop="phone"
        >
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm('form')">提交</el-button>
          <el-button @click="closeInfo('form')">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { studentInfo, info, delInfo } from '@/api/api'
import PageWeb from '../common/PageWeb.vue'

export default {
  components: {
    PageWeb
  },
  data () {
    return {
      tableData: [],
      total: 0,
      comData: [],
      dialogFormVisible: false,
      formLabelWidth: '100px',
      state: true,
      loading: true,
      form: {
        name: '',
        sex: '1',
        age: '',
        father: '',
        mother: '',
        time: '',
        address: '',
        phone: '',
      },
      rules: {
        name: [{ required: true, message: '请输入姓名' }],
        sex: [{ required: true }],
        age: [{ required: true, message: '请输入年龄' }],
        time: [{ required: true, message: '请输入地址' }],
        address: [{ required: true, message: '请输入入学时间' }],
        phone: [{ required: true, message: '请输入联系方式' }],
      }
    }
  },
  methods: {
    getTableData (val) {
      this.comData = val
    },
    edit (row) {
      this.state = false
      console.log(row)
      this.form = { ...row }
      this.dialogFormVisible = true
    },
    del (row) {
      this.$alert("确定要删除吗？", "提示", {
        confirmButtonText: '确定',
        callback: () => {
          delInfo(row.id).then(res => {
            if (res.status === 200) {
              this.getData()
              this.$message({ message: res.data.message, type: "success" })
            }
          })
        }
      })
    },
    getData (params) {
      studentInfo(params).then(res => {
        if (res.data.status === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
          this.loading = false
          this.tableData.forEach((item) => {
            // 尽量不要修改原数据
            item.sex === '1' ? (item.sex_text = "男") : (item.sex_text = "女")
          })
        }
      })
    },
    // 添加学生信息方法
    addStudent () {
      this.state = true
      this.form = {
        name: '',
        sex: '1',
        age: '',
        father: '',
        mother: '',
        time: '',
        address: '',
        phone: '',
      }
      this.dialogFormVisible = true
    },
    // 取消操作
    closeInfo (form) {
      this.$refs[form].resetFields()
      this.dialogFormVisible = false
    },
    // 提交表单
    submitForm (form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          if (this.state) {
            info('post', this.form).then(res => {
              if (res.data.status === 200) {
                this.$refs[form].resetFields()
                this.getData()
                this.dialogFormVisible = false
                this.$message({ type: 'success', message: res.data.message })
              }
            })
          } else {
            info('put', this.form).then(res => {
              if (res.data.status === 200) {
                this.getData()
                // 在弹窗关闭前重置
                this.$refs[form].resetFields()
                this.dialogFormVisible = false
                this.$message({ type: 'success', message: res.data.message })
              }
            })
          }
        }
      })
    }
  },
  created () {
    this.getData()
    this.getTableData
  }
}
</script>

<style lang="less">
.info-box {
  .add-stu {
    margin-bottom: 5px;
  }
  .el-form {
    .el-form-item {
      margin-bottom: 20px;
    }
  }
  .el-table {
    .operate {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      .el-button {
        font-size: 12px;
        width: 50px;
      }
    }
  }
}
</style>